package cz.czu.pef.DataModelling.entityObjects;

import java.util.*;

import javax.persistence.*;

/**
 * This entity represents EmployeeEntityImpl with its relation to employers (companies), person and
 * roles which employee represents in companies (s)he works in.
 * 
 * @author rstribrn
 *
 */
@Entity
@Table(name="EMPLOYEE")
@Inheritance(strategy=InheritanceType.JOINED)
/*
@NamedQueries({
	@NamedQuery(name="SortedEmployees", query="select e.person.firstName, e.person.lastName, e.person.birthDate from EmployeeEntityImpl e order by e.person.birthDate asc, e.person.lastName asc, e.person.firstName asc"),
	@NamedQuery(name="findEmployeeByName", query="SELECT e FROM EmployeeEntityImpl e WHERE e.person.firstName = :firstName AND e.person.lastName = :lastName")
	})
*/
//@DiscriminatorValue("EMPLOYEE")
//@DiscriminatorColumn(name="subclass", discriminatorType=DiscriminatorType.CHAR)
public class EmployeeEntityImpl extends IdPersistenceEntityObjectImpl implements IdPersistenceEntityObject
{
	private static final long serialVersionUID = -3272793380650077540L;

	/*
	 * Entity attributes
	 */
	@ManyToOne(fetch=FetchType.EAGER, cascade={CascadeType.PERSIST, CascadeType.MERGE, CascadeType.REFRESH}, targetEntity=PersonEntityImpl.class)
	private PersonEntityImpl person;

	/*
	 * Relations
	 */
	@ManyToMany(fetch=FetchType.EAGER, targetEntity=CompanyEntityImpl.class, mappedBy="employees") // no cascade!
	private Set<CompanyEntityImpl> employers;

	@ManyToMany(fetch = FetchType.EAGER, targetEntity = RoleEntityImpl.class, cascade={CascadeType.ALL}) // all cascades!
	@JoinTable(name="EMPLOYEE_ROLES")
	private Set<RoleEntityImpl> roles;

	public EmployeeEntityImpl()
	{
		super();
		person = new PersonEntityImpl();
	}

	public EmployeeEntityImpl(PersonEntityImpl person)
	{
		this.setPerson(person);
	}

	public Set<CompanyEntityImpl> getEmployers()
	{
		return employers;
	}

	public List<CompanyEntityImpl> getEmployersAsList()
	{
		return new ArrayList<CompanyEntityImpl>(getEmployers());
	}

	public void addEmployer(CompanyEntityImpl newEmployer)
	{
		if (employers == null)
			employers = new HashSet<CompanyEntityImpl>();
		employers.add(newEmployer);
	}

	public PersonEntityImpl getPerson()
	{
		return person;
	}

	public void setPerson(PersonEntityImpl person)
	{
		this.person = person;
	}

	public Set<RoleEntityImpl> getRoles()
	{
		return roles;
	}

	public List<RoleEntityImpl> getRolesAsList()
	{
		return new ArrayList<RoleEntityImpl>(getRoles());
	}

	public void addRole(RoleEntityImpl role)
	{
		if (roles == null)
			roles = new HashSet<RoleEntityImpl>();

		roles.add(role);
	}

    
  public void setEmployers(Set<CompanyEntityImpl> employers)
  {
    this.employers = employers;
  }

  
  public void setRoles(Set<RoleEntityImpl> roles)
  {
    this.roles = roles;
  }

    @Override
	public String toString()
	{
		StringBuilder builder = new StringBuilder();
		builder.append("EmployeeEntityImpl [person=");
		builder.append(person);
		builder.append(", roles=");
		builder.append(roles);
		builder.append(", toString()=");
		builder.append(super.toString());
		builder.append("]");
		return builder.toString();
	}
	
	/* (non-Javadoc)
	 * @see cz.czu.pef.DataModelling.domainObjects.Employee#canBeRemoved()
	 */
	@Override
	public boolean canBeRemoved()
	{
		return getEmployers().size() == 0;
	}
}